package com.servlets;

import com.domain.Movies;
import com.domain.service.MoviesService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateMoviesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String movieSearchName = request.getParameter("movieSearchName");

        MoviesService moviesService = new MoviesService();

        List<Movies> moviesList = moviesService.getAll();

        Movies movies = null;

        for (int i = 0; i < moviesList.size(); i++) {

            if (moviesList.get(i).getMovieName().equalsIgnoreCase(movieSearchName)) {

                movies = moviesList.get(i);

            }
        }

        if (movies == null) {
            return;
        }
        PrintWriter out = null;

        try {

            response.setContentType("text/html");

            out = response.getWriter();

            out.println("<html>");
            out.println("<title>AbhiApp | Movie</title>");
            out.println("<head></head>");
            out.println("<body bgcolor='gray'>");

             out.println("<h1 align='center'>Movie Details</h1><hr/><br>");
            out.println("<table bgcolor='white'align='center' cellspacing='40' >");
            out.println("<th>Language</th><th>Category</th><th>Description</th><th>Movie Name</th><th>year</th>");

            out.println("<tr><td>" + movies.getLanguage() + "</td>");
            out.println("<td>" + movies.getCategory() + "</td>");
            out.println("<td>" + movies.getDescription() + "</td>");
            out.println("<td>" + movies.getMovieName() + "</td>");
            out.println("<td>" + movies.getYear() + "</td>");
            out.println("</tr>");

            out.println("</table><br>");
            
            out.println("<p align='center'><a  href='index.html'>| HOME |</a></p>");
            out.println("</body>");
            out.println("</html>");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String language = request.getParameter("language");
        String category = request.getParameter("category");
        String description = request.getParameter("description");
        String movieName = request.getParameter("movieName");
        String year = request.getParameter("year");

        Movies movies = new Movies();

        movies.setLanguage(language);
        movies.setCategory(category);
        movies.setDescription(description);
        movies.setMovieName(movieName.toUpperCase());
        movies.setYear(year);

        MoviesService moviesService = new MoviesService();
        moviesService.create(movies);

        response.sendRedirect("index.html");

    }
}
